package com.dianping.cat.alarm.policy.transform;

import com.dianping.cat.alarm.policy.entity.AlertPolicy;
import com.dianping.cat.alarm.policy.entity.Group;
import com.dianping.cat.alarm.policy.entity.Level;
import com.dianping.cat.alarm.policy.entity.Type;
import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;






public class DefaultDomParser
  extends Object
  implements IParser<Node>
{
  protected Node getChildTagNode(Node parent, String name) {
/*  28 */     NodeList children = parent.getChildNodes();
    int len = children.getLength();

    for (int i = 0; i < len; i++) {
      Node child = children.item(i);

/*  34 */       if (child.getNodeType() == 1 && 
/*  35 */         child.getNodeName().equals(name)) {
        return child;
      }
    }


/*  41 */     return null;
  }

  protected List<Node> getChildTagNodes(Node parent, String name) {
/*  45 */     NodeList children = parent.getChildNodes();
/*  46 */     int len = children.getLength();
/*  47 */     List<Node> nodes = new ArrayList<Node>(len);

/*  49 */     for (int i = 0; i < len; i++) {
/*  50 */       Node child = children.item(i);

/*  52 */       if (child.getNodeType() == 1 && (
/*  53 */         name == null || child.getNodeName().equals(name))) {
/*  54 */         nodes.add(child);
      }
    }


/*  59 */     return nodes;
  }

  protected Node getDocument(String xml) {
/*  63 */     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

/*  65 */     dbf.setIgnoringElementContentWhitespace(true);
/*  66 */     dbf.setIgnoringComments(true);

    try {
/*  69 */       DocumentBuilder db = dbf.newDocumentBuilder();

/*  71 */       return db.parse(new InputSource(new StringReader(xml)));
/*  72 */     } catch (Exception x) {
/*  73 */       throw new RuntimeException(x);
    }
  }

  protected List<Node> getGrandChildTagNodes(Node parent, String name) {
    Node child = getChildTagNode(parent, name);
/*  79 */     NodeList children = (child == null) ? null : child.getChildNodes();
/*  80 */     int len = (children == null) ? 0 : children.getLength();
/*  81 */     List<Node> nodes = new ArrayList<Node>(len);

/*  83 */     for (int i = 0; i < len; i++) {
/*  84 */       Node grandChild = children.item(i);

/*  86 */       if (grandChild.getNodeType() == 1) {
/*  87 */         nodes.add(grandChild);
      }
    }

/*  91 */     return nodes;
  }


/*  95 */   public AlertPolicy parse(Node node) { return parse(new DefaultDomMaker(), new DefaultLinker(false), node); }


  public AlertPolicy parse(String xml) throws SAXException, IOException {
/*  99 */     Node doc = getDocument(xml);
/* 100 */     Node rootNode = getChildTagNode(doc, "alert-policy");

/* 102 */     if (rootNode == null) {
/* 103 */       throw new RuntimeException(String.format("alert-policy element(%s) is expected!", new Object[] { "alert-policy" }));
    }

/* 106 */     return parse(new DefaultDomMaker(), new DefaultLinker(false), rootNode);
  }

  public AlertPolicy parse(IMaker<Node> maker, ILinker linker, Node node) {
/* 110 */     AlertPolicy alertPolicy = maker.buildAlertPolicy(node);

/* 112 */     if (node != null) {
/* 113 */       AlertPolicy parent = alertPolicy;

/* 115 */       for (Node child : getChildTagNodes(node, "type")) {
/* 116 */         Type type = maker.buildType(child);

/* 118 */         if (linker.onType(parent, type)) {
/* 119 */           parseForType(maker, linker, type, child);
        }
      }
    }

/* 124 */     return alertPolicy;
  }

  public void parseForGroup(IMaker<Node> maker, ILinker linker, Group parent, Node node) {
/* 128 */     for (Node child : getChildTagNodes(node, "level")) {
/* 129 */       Level level = maker.buildLevel(child);

/* 131 */       if (linker.onLevel(parent, level)) {
/* 132 */         parseForLevel(maker, linker, level, child);
      }
    }
  }


  public void parseForLevel(IMaker<Node> maker, ILinker linker, Level parent, Node node) {}

  public void parseForType(IMaker<Node> maker, ILinker linker, Type parent, Node node) {
/* 141 */     for (Node child : getChildTagNodes(node, "group")) {
/* 142 */       Group group = maker.buildGroup(child);

/* 144 */       if (linker.onGroup(parent, group))
/* 145 */         parseForGroup(maker, linker, group, child); 
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-alarm-3.0.0.jar!/com/dianping/cat/alarm/policy/transform/DefaultDomParser.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */